package com.xiaozhichao.databafflesys.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.Map;

/**
 * @author:xiaozhichao
 * @date:2021/3/29
 */
public class ReqOrder {

    public static String queryString(String input){
        if(StringUtils.isBlank(input)){
            return null;
        }
        String[] split = input.split("&");
        JSONObject json = new JSONObject();
        for (String s:split){
            int i = s.lastIndexOf("=");
            json.put(s.substring(0,i),s.substring(i+1));
        }
        return json.toJSONString();
    }

    public static String formData(Map<String, String[]> parameterMap){
        if(MapUtils.isEmpty(parameterMap)){
            return null;
        }
        JSONObject json = new JSONObject();
        Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String, String[]> next = iterator.next();
            json.put(next.getKey(),next.getValue()[0]);
        }
        return json.toJSONString();
    }



    public static void main(String[] args) {
        System.out.println(queryString("1=1&a=2&b=3"));
    }
}
